package com.sff.easyexcel.test.demo.template;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sff.easyexcel.test.demo.merge.CellMergeStrategy;
import com.sff.easyexcel.test.demo.util.FilePathUtil;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考试成表导出
 * 1.不创建对象的方式导出
 * https://blog.csdn.net/u010425898/article/details/106692567
 */
public class ExamResultData {


    public static void main(String[] args) {
        List<ScoreReport> exportDataList = getExportData();
        List<String> classList = exportDataList.stream().map(ScoreReport::getClassName).distinct().collect(Collectors.toList());

        List<List<Object>> dataList = Lists.newArrayList();
        exportDataList.forEach(d -> dataList.add(
                Lists.newArrayList(d.getClassName(), d.getStudentNo(), d.getName(), d.getSubject(), d.getChoiceQuestion(), d.getReadingQuestion())
        ));
        String writeFileName = FilePathUtil.getPath() + "score-" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(writeFileName)
                .head(createHead(classList))
                .registerWriteHandler(new CellMergeStrategy(0,0, Sets.newHashSet(1, 2)))
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("班级成绩表")
                .doWrite(dataList);
    }

    private static List<ScoreReport> getExportData() {
        List<ScoreReport> dataList = Lists.newArrayList();
        dataList.add(new ScoreReport("高一三班", "A001", "小米", "语文", 10, 20));
        dataList.add(new ScoreReport("高一三班", "A001", "小米", "数学", 13, 26));
        dataList.add(new ScoreReport("高一三班", "A002", "小金", "语文", 15, 30));
        dataList.add(new ScoreReport("高一三班", "A002", "小金", "数学", 20, 40));
        dataList.add(new ScoreReport("高一五班", "A003", "小金", "语文", 61, 70));
        dataList.add(new ScoreReport("高一五班", "A003", "小Q", "数学", 21, 40));
        return dataList;
    }

    private static List<List<String>> createHead(List<String> clazz) {
        List<List<String>> list = Lists.newArrayList();

        List<String> head0 = Lists.newArrayList();
        String title = "班级分数表";
        String clazzInfo = "包含（" + Joiner.on(",").join(clazz) + "）";
        head0.add(title);
        head0.add(clazzInfo);
        head0.add("班级");
        list.add(head0);

        List<String> head = Lists.newArrayList();
        head.add(title);
        head.add(clazzInfo);
        head.add("学号");
        list.add(head);

        List<String> head1 = Lists.newArrayList();
        head1.add(title);
        head1.add(clazzInfo);
        head1.add("姓名");
        list.add(head1);

        List<String> head2 = Lists.newArrayList();
        head2.add(title);
        head2.add(clazzInfo);
        head2.add("学科");
        list.add(head2);

        List<String> head3 = Lists.newArrayList();
        head3.add(title);
        head3.add(clazzInfo);
        head3.add("选择题");
        list.add(head3);

        List<String> head4 = Lists.newArrayList();
        head4.add(title);
        head4.add(clazzInfo);
        head4.add("阅读题");
        list.add(head4);

        return list;
    }

    /**考试分数*/
    @Data
    private static class ScoreReport {
        /**班级*/
        private String className;
        /**学号*/
        private String studentNo;
        /**学生姓名*/
        private String name;
        /**科目*/
        private String subject;
        /**选择题得分*/
        private int choiceQuestion;
        /**阅读题*/
        private int readingQuestion;

        public ScoreReport() {
        }

        public ScoreReport(String className, String studentNo, String name, String subject, int choiceQuestion, int readingQuestion) {
            this.className = className;
            this.studentNo = studentNo;
            this.name = name;
            this.subject = subject;
            this.choiceQuestion = choiceQuestion;
            this.readingQuestion = readingQuestion;
        }
    }
}
